package sjtu.ist.warehouse4ws.action;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import sjtu.ist.warehouse4ws.model.Column;
import sjtu.ist.warehouse4ws.model.ColumnValue;
import sjtu.ist.warehouse4ws.service.ColumnService;
import sjtu.ist.warehouse4ws.service.ColumnValueService;
import sjtu.ist.warehouse4ws.service.cust.CustColumnService;
import sjtu.ist.warehouse4ws.service.cust.CustColumnValueService;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.apache.struts2.json.annotations.JSON;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class ColumnValueAction extends ActionSupport {
	//service
	CustColumnValueService custColumnValueService;
	ColumnValueService columnValueService;	
	CustColumnService custColumnService;
	ColumnService columnService;
	
	//output
	List<ColumnValue> columnValueList=new ArrayList<ColumnValue>();
	String ColumnValues="";
	
	//input
	String tablename;
	String columnname;
	int type;
	
	public String showTableValue(){
		Subject currentUser = SecurityUtils.getSubject();
		
		if(!currentUser.hasRole("superadmin")){
			columnValueList=custColumnValueService.getByTable("cust_"+tablename);
		}else{
			columnValueList=columnValueService.getByTable(tablename);
		}
		
		
		//ColumnValues=formatJson();
		
		return SUCCESS;
	}
	
/*
	public String formatJson(){
		if(columnValueList.size()<=0){
			return "[]";
		}
		
		
		
		List<Column> columnList;
		if(type!=0){
			columnList=custColumnService.getByTable("cust_"+tablename);
		}else{
			columnList=columnService.getByTable(tablename);
		}
		if(columnList.size()<=0){
			return "[]";
		}
		
		HashMap<Integer,String> nameMap=new HashMap<Integer,String> ();
		for(int i=0;i<columnList.size();i++){
			nameMap.put(columnList.get(i).getId(), columnList.get(i).getColumnname());
		}
		
		int len=columnList.size()+1;	
		ArrayList<HashMap<String,String>> vlist=new ArrayList<HashMap<String,String>>();
		
		StringBuilder str=new StringBuilder("[");
		for(int i=0;i<columnValueList.size();i++){
			ColumnValue cv=columnValueList.get(i);
			boolean exist=false;
			for(int j=0;j<vlist.size();j++){
				HashMap<String,String> v=vlist.get(j);
				if(v.get("id").equals(cv.getRecordId())){
					v.put(nameMap.get(cv.getColumnId()), cv.getValue());
					exist=true;
					break;
				}				
			}
			if(!exist){
				HashMap<String,String> v=new HashMap<String,String>();
				v.put("id", cv.getRecordId());
				v.put(nameMap.get(cv.getColumnId()), cv.getValue());
				vlist.add(v);
			}			
		}
		
		for(int i=0;i<vlist.size();i++){
			HashMap<String,String> v=vlist.get(i);
			if(i==0){
				str.append("{'id':'");
			}else{
				str.append(",{'id':'");
			}
			
			str.append(v.get("id"));
			str.append("'");
			Set<String> keys=v.keySet();
			for(String key : keys){
				str.append(",'");
				str.append(key);
				str.append("':'");
				str.append(v.get(key));
				str.append("'");
			}
			str.append("}");
		}
		
		str.append("]");
		return str.toString();
	}	
*/	
	
	@JSON(name = "columnValue")
	public List<ColumnValue> getColumnValueList() {
		return columnValueList;
	}

	
	public String getColumnValues() {
		return ColumnValues;
	}

	@JSON(name = "total")
	public int getTotalColumnValueList() {
		return columnValueList.size();
	}

	public void setCustColumnValueService(CustColumnValueService custColumnValueService) {
		this.custColumnValueService = custColumnValueService;
	}

	public void setColumnValueService(ColumnValueService columnValueService) {
		this.columnValueService = columnValueService;
	}
	
	public void setCustColumnService(CustColumnService custColumnService) {
		this.custColumnService = custColumnService;
	}

	public void setColumnService(ColumnService columnService) {
		this.columnService = columnService;
	}

	public void setTablename(String tablename) {
		this.tablename = tablename;
	}

	public void setColumnname(String columnname) {
		this.columnname = columnname;
	}

	public void setType(int type) {
		this.type = type;
	}
}